package login;

import quesiton.SettingOp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.*;


public class dbhelper {
    Connection connection;

    public dbhelper() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String user = "root";
            String pass = "123456";
            String database = "mysql";
            connection = DriverManager.getConnection("jdbc:mysql://1.14.64.237:3306/" + database + "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true", user, pass);
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            //sjkljsb sb = new sjkljsb();
            System.out.println("数据库连接失败\n" + e.toString());
        }
    }

    public void close() {
        try {
            this.connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void login(String name, String passwords, JFrame f) {
        String sql = "SELECT * FROM user1 WHERE name ='" + name + "'and passwords='" + passwords + "'";
        try {
            Statement stmt = (Statement) this.connection.createStatement();
            ResultSet rs = (ResultSet) stmt.executeQuery(sql);
            if (rs.next() == true) {
                f.setVisible(false);
                JOptionPane.showMessageDialog(null, "登录成功！", "提示窗口", JOptionPane.INFORMATION_MESSAGE);
                SettingOp.settings();
            } else {
                JOptionPane.showMessageDialog(null, "登录失败：请检查用户和密码是否正确！", "提示窗口", JOptionPane.WARNING_MESSAGE);
            }
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *提交所有试题
     */
    public void submit(ArrayList<String> list ,ArrayList<Float> answer) {

        for (String s : list) {
            System.out.println(s);
        }
        for (Float in : answer) {
            System.out.println(in+"");
        }
        for (int i = 0; i < list.size(); i++) {
            String sql = "insert into question values('" + list.get(i) + "','" + answer.get(i).toString() + "')";
            try {
                PreparedStatement preStmt = (PreparedStatement) this.connection.prepareStatement(sql);
                preStmt.executeUpdate();
                System.out.println("插入数据成功！");
                preStmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void add(String name, String passwords) {
        String sql = "insert into user1(name,passwords) values('" + name + "','" + passwords + "')";
        try {
            PreparedStatement preStmt = (PreparedStatement) this.connection.prepareStatement(sql);
            preStmt.executeUpdate();
            System.out.println("账号注册成功！");
            preStmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void update(String name, String passwords) {
        String sql = "update us set passwords=? where name=?";
        try {
            PreparedStatement prestmt = (PreparedStatement) this.connection.prepareStatement(sql);
            prestmt.setString(2, name);
            prestmt.setString(1, passwords);
            prestmt.executeUpdate();
            System.out.println("更改数据成功！");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
